//
//  MLAShopFoodChooseTypeVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/16.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAShopFoodChooseTypeVC.h"
#import "MLAEateryFoodTypeCell.h"
#import "MLAEateryFoodTypeModel.h"

@interface MLAShopFoodChooseTypeVC ()
@property (nonatomic, strong) NSMutableArray<MLAEateryFoodTypeModel *> *modelsArr;

@end

@implementation MLAShopFoodChooseTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"商品类型"];
    
    self.tableView.rowHeight = 60;
    [self.tableView registerClass:[MLAEateryFoodTypeCell class] forCellReuseIdentifier:kCellID];
    self.haveRefreshHeader = YES;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    NSDictionary *params = @{@"type":@"2"};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kProductProductTypeList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self.modelsArr = [NSMutableArray array];
        self.modelsArr = [MLAEateryFoodTypeModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self.modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
    } failure:^{
        
    }];
    
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page),
                             @"type":@"2"};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kProductProductTypeList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.modelsArr addObjectsFromArray:[MLAEateryFoodTypeModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAEateryFoodTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
    cell.editImgv.hidden = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    MLAEateryFoodTypeModel *model = self.modelsArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MLAEateryFoodTypeModel *model = self.modelsArr[indexPath.row];
    _chooseTypeBlock(model);
    [self.navigationController popViewControllerAnimated:YES];
}



@end
